﻿
Imports std
Imports urlUtils
Imports xmlctrl

Public Module xmlUtils
    Private xmlparserPool As xmlparserPoolDefinition = Nothing

    Public Function readHttpXml(ByVal address As String _
                                , ByRef response As String, ByRef root As xmlcontrol) As Boolean
        If readHttpWeb(address, response) Then
            Dim parser As parser = Nothing
            Dim index As Int64
            If xmlparserPool.getfree(index, parser) Then
                Dim rst As Boolean = False
                rst = parser.parse(response, root)
                If Not rst Then
                    raiseError("cannot parse response, ignore request. " + display(response) _
                               , errorHandle.errorType.warning)
                ElseIf root Is Nothing Then
                    raiseError("parse successfully, but did not return root node. " + strleft(response, 64) + "..." _
                               , errorHandle.errorType.warning)
                End If

                xmlparserPool.release(index)
                Return rst AndAlso Not root Is Nothing
            Else
                raiseError("cannot parse response, ignore request. " + display(response) _
                           , errorHandle.errorType.warning)
                Return False
            End If
        Else
            'do not need messagebox here, readHttpWeb will handle it
            If errorHandle.mode = errorHandle.displayMode.console Then
                raiseError("cannot get http response from address " + address, errorHandle.errorType.warning)
            End If
            Return False
        End If
    End Function

    Public Function readHttpXml(ByVal baseaddress As String _
                                , ByRef response As String, ByRef root As xmlcontrol _
                                , ByVal arguments As vector(Of String)) As Boolean
        Return readHttpXml(appendQuery(baseaddress, arguments), response, root)
    End Function

    Public Function readHttpXml(ByVal baseaddress As String _
                                , ByRef response As String, ByRef root As xmlcontrol _
                                , ByVal arguments As vector(Of pair(Of String, String)) _
                                , Optional ByVal encoding As Text.Encoding = Nothing) As Boolean
        Return readHttpXml(appendQuery(baseaddress, arguments, encoding), response, root)
    End Function

    Public Function readHttpXml(ByVal baseaddress As String _
                                 , ByRef response As String, ByRef root As xmlcontrol _
                                 , ByVal ParamArray arguments() As String) As Boolean
        Return readHttpXml(appendQuery(baseaddress, arguments), response, root)
    End Function

    Public Function readHttpXml(ByVal baseaddress As String _
                                , ByRef response As String, ByRef root As xmlcontrol _
                                , ByVal encoding As Text.Encoding _
                                , ByVal ParamArray arguments() As String) As Boolean
        Return readHttpXml(appendQuery(baseaddress, encoding, arguments), response, root)
    End Function

    Sub New()
        xmlparserPool = New xmlparserPoolDefinition()
    End Sub
End Module
